package com.mobzilla.activity;

import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.mobzilla.model.Bug;
import com.mobzilla.model.Comment;
import com.mobzilla.model.Status;
import com.mobzilla.model.User;
import com.mobzilla.service.ServiceFactory;
import com.mobzilla.util.Constants;

public class SaveActivity extends Activity implements OnClickListener
{
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.save);

		Spinner s = (Spinner) findViewById(R.id.spinner1);
		
		ArrayAdapter adapter = ArrayAdapter.createFromResource(
						this, 
						R.array.statusAvailable, 
						android.R.layout.simple_spinner_item);
		
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		
		s.setAdapter(adapter);
		
		initListeners();
	}
	
	private void initListeners()
	{
		Button saveButton = (Button)findViewById(R.id.saveButton);
		
		saveButton.setOnClickListener(new SaveButtonListener(this));
		
		Button backButton = (Button)findViewById(R.id.saveBackButton);
		
		backButton.setOnClickListener(new BackButtonListener(this));
		
		Button searchButton = (Button)findViewById(R.id.saveSearchButton);
		
		searchButton.setOnClickListener(this);
	}
	
	public void onClick(View v)
	{
		TextView bugNumberTextView = (TextView)findViewById(R.id.saveBugNumberTextField);
		try
		{
			int bugID = Integer.parseInt(bugNumberTextView.getText().toString());
			
			Bug bug = ServiceFactory.getSearchServiceInstance().fetchDefectDetails(bugID);
			
			if(bug == null)
			{
				Toast.makeText(this, "Bug not found", Toast.LENGTH_LONG).show();
			}
			else
			{
				SaveButtonListener.setCurrentBug(bug);
				
				populateView(bug);
			}
		}
		catch (Exception e)
		{
			Toast.makeText(this, "Invalid Input", Toast.LENGTH_LONG).show();
		}		
	}
	
	private void populateView(Bug b)
	{
		TextView bugSummaryTextView = (TextView)findViewById(R.id.tvBugSummary);
		
		bugSummaryTextView.setText(b.getSummary());
		
		Spinner s = (Spinner) findViewById(R.id.spinner1);
		
		s.setSelection(Status.getStatusFromString(b.getStatus()).ordinal());
		
		TextView bugDetailsTextView = (TextView)findViewById(R.id.saveBugDetailsTextField);
		
		bugDetailsTextView.setText(b.getDetails());
	}
}

class BackButtonListener implements OnClickListener
{
	private Activity activity;
	
	public BackButtonListener(Activity a)
	{
		this.activity = a;
	}
	
	public void onClick(View v)
	{
		activity.finish();
	}
}


class SaveButtonListener implements OnClickListener
{
	private static Bug currentBug;
	
	private Activity activity;
	
	public SaveButtonListener(Activity a)
	{
		this.activity = a;
	}
	
	public void onClick(View v)
	{
		try
		{
		TextView bugNumberTextView = (TextView)activity.findViewById(R.id.saveBugNumberTextField);
		int bugID = Integer.parseInt(bugNumberTextView.getText().toString());

		Spinner s = (Spinner) activity.findViewById(R.id.spinner1);
		
		currentBug.setStatus(s.getSelectedItem().toString());
		
		List<Comment> comments = currentBug.getComments();
		
		TextView comment = (TextView)activity.findViewById(R.id.addCommentsTextField);
		
		if(comment.getText() != null || comment.getText().length() > 0)
		{
			Comment c = new Comment();
			c.setText(comment.getText().toString());
			
			User creator = new User();
			creator.setName(Constants.getCurrentUser().getUsername());
			
			c.setCreator(creator);
			
			ServiceFactory.getCreateServiceInstance().addComment(
							currentBug.getId(), 
							c, 
							Constants.getCurrentUser().getUsername(), 
							Constants.getCurrentUser().getUsername());
		}
		
		currentBug.setComments(comments);
		
		if (!ServiceFactory.getUpdateServiceInstance().updateStatus(
						Status.getStatusFromString(currentBug.getStatus()), 
						currentBug.getId(), 
						Constants.getCurrentUser().getUsername(), 
						Constants.getCurrentUser().getUsername()))
		{
			Toast.makeText(activity, "Unable to update bug", Toast.LENGTH_LONG).show();
		}
		else
		{
			Toast.makeText(activity, "Request sent to server successfully", Toast.LENGTH_LONG).show();
		}
		}
		catch (Exception e)
		{
			Toast.makeText(activity, "Invalid Input", Toast.LENGTH_LONG).show();
		}
	}
	
	public static void setCurrentBug(Bug b)
	{
		currentBug = b;
	}
}